package lib.core.view
{
	import flash.events.Event;
	
	import lib.config.ApplicationNotifications;
	import lib.core.com.BaseApplication;
	
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import urlkit.rules.FlexBrowserManagerAdapter;
	
	public class BaseApplicationMediator extends Mediator implements IMediator
	{
		public function get application():BaseApplication
		{
			return this.viewComponent as BaseApplication;
		}
		
		public function BaseApplicationMediator(name:String, viewComponent:BaseApplication)
		{
			super(name, viewComponent);
			FlexBrowserManagerAdapter.getInstance().addEventListener(FlexEvent.INIT_COMPLETE, onControllerAvailable);
		}
		
		protected function onControllerAvailable(e:FlexEvent):void
		{
			this.application.route.addEventListener(Event.CHANGE, onUrlChange);
			this.sendNotification(ApplicationNotifications.STARTUP_COMPLETE, this.application);
			this.sendUrlValidate();
		}
		
		protected function onUrlChange(e:Event):void
		{
			this.sendUrlValidate();			
		}
		
		protected function sendUrlValidate():void
		{
			this.sendNotification(ApplicationNotifications.URLRULE_VALIDATE, FlexBrowserManagerAdapter.getInstance().browserUrl);
		}
	}
}